package com.yeping.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeping.constants.SystemConstants;
import com.yeping.domain.ResponseResult;
import com.yeping.domain.entity.Comment;
import com.yeping.domain.vo.CommentVo;
import com.yeping.domain.vo.PageVo;
import com.yeping.enums.AppHttpCodeEnum;
import com.yeping.exception.SystemException;
import com.yeping.mapper.CommentMapper;
import com.yeping.service.CommentService;
import com.yeping.service.UserService;
import com.yeping.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-01-27 20:05:43
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commonList(String comentType, Long articleId, Integer pageNum, Integer pageSize, String content) {
        //查询对应文章根评论

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //对articleId进行判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMENT.equals(comentType),Comment::getArticleId,articleId);

        //根评论 rootId为-1
        queryWrapper.eq(Comment::getRootId, SystemConstants.COMMENT_ROOT);

        //评论类型
        queryWrapper.eq(Objects.nonNull(comentType),Comment::getType,comentType);

        //搜索
        queryWrapper.like(Objects.nonNull(content),Comment::getContent,content);

        //分页查询
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Comment> records = page.getRecords();


        List<CommentVo> commentVos = getCommentVos(page.getRecords());
        //查询根评论下的所有子评论合集
        for (CommentVo commentVo : commentVos) {
            //查询对应子评论
            List<CommentVo> commentVos1 = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(commentVos1);
        }


        return ResponseResult.okResult(new PageVo(commentVos,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
//        comment.setCreateBy(SecurityUtils.getUserId());
        //此处mybatisplus会自动填充字段
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return commonList2("0",comment.getArticleId());
    }

    @Override
    public ResponseResult commonList2(String comentType, Long articleId) {
        //查询对应文章根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //对articleId进行判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMENT.equals(comentType),Comment::getArticleId,articleId);
        //根评论 rootId为-1
        queryWrapper.eq(Comment::getRootId, SystemConstants.COMMENT_ROOT);
        //评论类型
        queryWrapper.eq(Comment::getType,comentType);
        //查询所有评论
        List<Comment> list = list(queryWrapper);
        List<CommentVo> commentVos = getCommentVos(list);
        //查询根评论下的所有子评论合集
        for (CommentVo commentVo : commentVos) {
            //查询对应子评论
            List<CommentVo> commentVos1 = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(commentVos1);

        }
        return ResponseResult.okResult(commentVos);
    }

    @Override
    public ResponseResult comLike(Comment comment) {
        updateById(comment);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateComment(Comment comment) {
        updateById(comment);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteComment(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delBatchComment(List<Long> ids) {
        removeByIds(ids);
        return ResponseResult.okResult();
    }

    /**
     * 根据根评论的id查询子评论
     * 根评论的id
     * @param id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        List<CommentVo> commentVos = getCommentVos(comments);
        return commentVos;
    }



    private List<CommentVo> getCommentVos(List<Comment> comments) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(comments,
                CommentVo.class);
        //便利list集合
        for (CommentVo commentVo : commentVos) {
            //通过createby查询username
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            String avatar = userService.getById(commentVo.getCreateBy()).getAvatar();
            commentVo.setAvatar(avatar);
            //通过toCommonuserid 查询昵称
            if (commentVo.getToCommentUserId() != -1){
                String nickName1 = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(nickName1);
            }

        }
        return commentVos;
    }
}
